package com.doupi.partner.service.impl;

import com.doupi.partner.dal.PartnerRelationMapper;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PartnerRelationServiceImpl implements PartnerRelationService {
	private final PartnerRelationMapper partnerRelationMapper;

	@Autowired
	public PartnerRelationServiceImpl(PartnerRelationMapper partnerRelationMapper) {
		this.partnerRelationMapper = partnerRelationMapper;
	}

	public int deleteByPrimaryKey(Long id) {
		return partnerRelationMapper.deleteByPrimaryKey(id);
	}

	public int insert(PartnerRelation record) {
		return partnerRelationMapper.insert(record);
	}

	public PartnerRelation selectByPrimaryKey(Long id) {
		return partnerRelationMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(PartnerRelation record) {
		return partnerRelationMapper.updateByPrimaryKeySelective(record);
	}

	public int countPartnerRelation(PartnerRelation partnerRelation) {
		return partnerRelationMapper.countPartnerRelation(partnerRelation);
	}

	public List<PartnerRelation> queryPartnerRelations(PartnerRelation partnerRelation, RowBounds rowBounds) {
		return partnerRelationMapper.queryPartnerRelations(partnerRelation, rowBounds);
	}

	public List<PartnerRelation> selectPartnerRelationForUpdate(PartnerRelation partnerRelation) {
		return partnerRelationMapper.selectPartnerRelationForUpdate(partnerRelation);
	}

	public PartnerRelation selectPartnerRelationForUpdateByPrimaryKey(Long id) {
		return partnerRelationMapper.selectPartnerRelationForUpdateByPrimaryKey(id);
	}

	public Pagination<PartnerRelation, PartnerRelation> queryPartnerRelations(Pagination<PartnerRelation, PartnerRelation> pagination) {
		int count = countPartnerRelation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryPartnerRelations(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public int savePartnerRelation(PartnerRelation partnerRelation) {
		int ret;
		if (partnerRelation.getId() == null || partnerRelation.getId() <= 0) {
			ret = insert(partnerRelation);
		} else {
			ret = updateByPrimaryKeySelective(partnerRelation);
		}
		return ret;
	}

	@Override
	public int countPartnerRelationWithAnnotation(PartnerRelation partnerRelation) {
		return partnerRelationMapper.countPartnerRelationWithAnnotation(partnerRelation);
	}

	@Override
	public List<PartnerRelation> queryPartnerRelationsWithAnnotation(PartnerRelation partnerRelation, RowBounds rowBounds) {
		return partnerRelationMapper.queryPartnerRelationsWithAnnotation(partnerRelation, rowBounds);
	}

	@Override
	public Pagination<PartnerRelation, PartnerRelation> queryPartnerRelationsWithAnnotation(Pagination<PartnerRelation, PartnerRelation> pagination) {
		int count = countPartnerRelationWithAnnotation(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryPartnerRelationsWithAnnotation(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public PartnerRelation selectByAdministratorId(Long administratorId) {
		PartnerRelation partnerRelation = new PartnerRelation();
		partnerRelation.setAdministratorId(administratorId);
		List<PartnerRelation> list = queryPartnerRelations(partnerRelation, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PartnerRelation selectByMarketNumber(String marketNumber) {
		PartnerRelation partnerRelation = new PartnerRelation();
		partnerRelation.setMarketingNumber(marketNumber);
		List<PartnerRelation> list = queryPartnerRelations(partnerRelation, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public PartnerRelation selectByOpenId(String openId) {
		PartnerRelation partnerRelation = new PartnerRelation();
		partnerRelation.setOpenId(openId);
		List<PartnerRelation> list = queryPartnerRelations(partnerRelation, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
}
